package com.jic.order.controller.manage;

import com.github.pagehelper.PageInfo;
import com.jic.common.base.vo.RestResult;
import com.jic.order.entity.*;
import com.jic.order.enums.OrderStatusEnum;
import com.jic.order.model.entity.MOrderBale;
import com.jic.order.request.*;
import com.jic.order.response.OrderClosedInfo;
import com.jic.order.response.OrderGoodsInfo;
import com.jic.order.response.OrderInfo;
import com.jic.order.response.OrderInvoceInfo;
import com.jic.order.response.OrderMarkInfo;
import com.jic.common.base.vo.PageResult;
import com.jic.order.service.*;
import com.jic.order.utils.BeanUtil;
import com.jic.product.after.api.ProductListApi;
import com.jic.product.after.response.ProductAfterInfoByIdResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Slf4j
@Api(tags = "后台-订单管理")
@RequestMapping(value = "orderManager")
@RestController
public class OrderManagerController {

	@Autowired
	private OrderService orderService;
	@Autowired
	private OrderOperateService orderOperateService;
	@Resource
	private ProductListApi productListApi;
	@Autowired
	private OrderGoodsService orderGoodsService;
	@Autowired
	private OrderMarkService orderMarkService;
	@Autowired
    private OrderPayDetailService orderPayDetailService;

	@ApiOperation(value = "订单分页查询", notes = "分页查询订单信息")
	@RequestMapping(value = "queryPageOrder", method = RequestMethod.POST)
	public RestResult<PageInfo<Order>> queryPageOrder(@RequestBody OrderQueryPage orderQueryPage) {
		return RestResult.success(orderService.queryPageOrder(orderQueryPage));
	}


	@ApiOperation(value = "订单信息", notes = "订单信息查询")
	@RequestMapping(value = "info", method = RequestMethod.POST)
	public RestResult<OrderInfo> info(@RequestBody OrderQueryPage orderQueryPage) {
		log.info("开始调用订单信息查询，{}", orderQueryPage.getOrderNo());
		RestResult<OrderInfo> result = orderService.info(orderQueryPage.getOrderNo());
		return result;
	}
	@ApiOperation(value = "包裹信息", notes = "包裹查询")
	@RequestMapping(value = "getParcel", method = RequestMethod.POST)
	public RestResult<MOrderBale> getParcel(@RequestBody OrderQueryPage orderQueryPage) {
		log.info("开始调用订单信息查询，{}", orderQueryPage.getOrderNo());
		RestResult<MOrderBale> result = orderService.getParcel(orderQueryPage.getOrderNo());
		return result;
	}



	@ApiOperation(value = "修改订单标签", notes = "修改订单标签")
	@RequestMapping(value = "modifyFlag", method = RequestMethod.POST)
	public RestResult modifyFlag(@RequestBody OrderTagRequest orderTagRequest) {
		RestResult result = orderService.modifyFlag(orderTagRequest);
		return result;
	}

	@ApiOperation(value = "订单的商品明细查询")
	@RequestMapping(value = "orderGoodsQuery", method = RequestMethod.POST)
	public RestResult<PageResult<OrderGoods>> orderGoodsQuery(@RequestBody OrderNoPage orderNoPage) {
		return orderService.queryOrderGoods(orderNoPage);
	}

	@ApiOperation(value = "订单的操作日志查询")
	@RequestMapping(value = "orderProcessLogQuery", method = RequestMethod.POST)
	public RestResult<PageResult<OrderProcessLog>> orderProcessLogQuery(@RequestBody OrderNoPage orderNoPage) {
		return orderService.orderProcessLogQuery(orderNoPage);
	}

    @ApiOperation(value = "订单的支付信息")
    @RequestMapping(value = "orderPayDetailQuery", method = RequestMethod.POST)
    public RestResult<PageResult<OrderPayDetail>> orderPayDetailQuery(@RequestBody OrderNoPage orderNoPage) {
        return orderService.orderPayDetailQuery(orderNoPage);
    }
    @ApiOperation(value = "订单的售后信息")
    @RequestMapping(value = "orderAfterSaleApplyQuery", method = RequestMethod.POST)
    public RestResult<PageResult<OrderAfterSaleApplyModel>> orderAfterSaleApplyQuery(@RequestBody OrderNoPage orderNoPage) {
        return orderService.orderAfterSaleApplyQuery(orderNoPage);
    }

	@ApiOperation(value = "订单的取消信息查询")
	@RequestMapping(value = "orderClosedInfoQuery", method = RequestMethod.POST)
	public RestResult<OrderClosedInfo> orderClosedInfoQuery(@RequestBody OrderNoPage orderNoPage) {
		return orderService.orderClosedInfoQuery(orderNoPage);
	}

	@ApiOperation(value = "发票信息查询")
	@RequestMapping(value = "invoceQuery", method = RequestMethod.POST)
	public RestResult<OrderInvoceInfo> invoceQuery(@RequestBody OrderNoPage orderNoPage) {
		return orderService.invoiceQuery(orderNoPage);
	}

	@ApiOperation(value = "附件及备注查询")
	@RequestMapping(value = "markQuery", method = RequestMethod.POST)
	public RestResult<OrderMarkInfo> markQuery(@RequestBody OrderNoPage orderNoPage) {
		return orderService.markQuery(orderNoPage);
	}

	@ApiOperation(value = "附件及备注保存")
	@RequestMapping(value = "saveMarkData", method = RequestMethod.POST)
	public RestResult<OrderMarkInfo> saveMarkData(@RequestBody OrderMarkRequest orderMarkRequest) {
		return orderMarkService.saveMarkData(orderMarkRequest);
	}

	@ApiOperation(value = "手动转交货")
	@RequestMapping(value = "handDeliver", method = RequestMethod.POST)
	public RestResult<String> handDeliver(@RequestBody HandDeliverRequest handDeliverRequest) {
		return orderOperateService.handDeliver(handDeliverRequest);
	}

	@ApiOperation(value = "订单审核")
	@RequestMapping(value = "audit", method = RequestMethod.POST)
	public RestResult<String> audit(@RequestBody OrderAuditRequest request) {
		return orderOperateService.audit(request);
	}

	@ApiOperation(value = "订单商品信息")
	@RequestMapping(value = "orderGoodsInfo", method = RequestMethod.POST)
	public RestResult<OrderGoods> orderGoodsInfo(@RequestBody OrderGoods orderGoodsModel) {
		OrderGoods goods = null;
		List<OrderGoods> query = orderGoodsService.query(orderGoodsModel);
		if (CollectionUtils.isNotEmpty(query)) {
			goods = query.get(0);
		}
		if (goods.getId() != 0) {
			RestResult<ProductAfterInfoByIdResponse> productAfterInfoByIdResponse = productListApi.queryProductInfoById(goods.getGoodsId());
			if (productAfterInfoByIdResponse != null) {
				if (StringUtils.isNotEmpty(productAfterInfoByIdResponse.getData().getPictureUrl())) {
					goods.setGoodsImgUrl(productAfterInfoByIdResponse.getData().getPictureUrl());
				}
				if (StringUtils.isNotEmpty(productAfterInfoByIdResponse.getData().getUnitName())) {
					goods.setUnitName(productAfterInfoByIdResponse.getData().getUnitName());
				}
				if (StringUtils.isNotEmpty(productAfterInfoByIdResponse.getData().getPropertyName())) {
					goods.setPropertyName(productAfterInfoByIdResponse.getData().getPropertyName());
				}
				if (StringUtils.isNotEmpty(productAfterInfoByIdResponse.getData().getCode())) {
					goods.setProductCode(productAfterInfoByIdResponse.getData().getCode());
				}
			}
		}

		return RestResult.success(goods);
	}

	@ApiOperation(value = "代客下单")
	@RequestMapping(value = "saveManualOrder", method = RequestMethod.POST)
	public RestResult<String> saveManualOrder(@RequestBody CreateParentOrderRequest createParentOrderRequest) {
		return orderOperateService.createOrder(createParentOrderRequest);
	}

	@ApiOperation(value = "根据订单编号查询订单商品")
	@RequestMapping(value = "queryOrderGoodsByOrderNo", method = RequestMethod.POST)
	public RestResult queryOrderGoodsByOrderNo(@RequestBody OrderGoodsRequest orderGoodsRequest) {
		OrderGoods orderGoods = BeanUtil.copy(orderGoodsRequest, OrderGoods.class);
		List<OrderGoods> orderGoodsList = orderGoodsService.query(orderGoods);
		List<OrderGoodsInfo> orderGoodsInfoList = orderGoodsList.stream().map(temp -> {
			OrderGoodsInfo orderGoodsInfo = BeanUtil.copy(temp, OrderGoodsInfo.class);
			return orderGoodsInfo;
		}).filter(item -> item != null).collect(Collectors.toList());

		return RestResult.success(orderGoodsInfoList);
	}


	@ApiOperation(value = "修改订单状态")
	@RequestMapping(value = "modifyOrderStatus", method = RequestMethod.POST)
	public RestResult queryOrderGoodsByOrderNo(@RequestBody OrderStatusAndNo OrderStatusAndNo) {
		Order order = BeanUtil.copy(OrderStatusAndNo, Order.class);
		
		//将状态设置为 已发货

		order.setOrderStatus(OrderStatusEnum.ALREADY_SHIP.getCode());
		orderService.update(order);
		return RestResult.success(order);
	}

}
